package club.mzywucai.blog.file_system.pojo.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;

import java.io.Serializable;
import java.util.List;

/**
 * @author 五彩世界
 * @Description
 * @date 2019/02/13
 */
public class PageBean<T> implements Serializable {

    private int firstPage = 1; // 首页：不需要手动输入
    private int prePage; // 上一页：不需要手动输入
    private int nextPage; // 下一页 ：不需要手动输入
    private int totalPage; // 总页数：不需要手动输入
    @JsonIgnore
    private int pageBeginIndex; // 数据库查询需要的页开始索引：不需要手动输入

    private List<T> content; // 当前页的数据：需要
    private int currentPage; // 当前页：需要
    private int totalCount; // 总记录数：需要
    private int pageSize; // 每页显示记录数：需要

    public PageBean(int currentPage, int totalCount, int pageSize) {
        super();

        this.totalCount = totalCount;
        this.pageSize = pageSize;

        // 1.算出totalPage
        if ( this.totalCount % this.pageSize == 0 )
            this.totalPage = this.totalCount / this.pageSize;
        else
            this.totalPage = this.totalCount / this.pageSize + 1;

        // 2.纠正好currentPage的上下限
        if ( currentPage < this.firstPage )
            this.currentPage = this.firstPage;
        else if ( this.currentPage > this.totalPage )
            this.currentPage = totalPage;
        else
            this.currentPage = currentPage;

        // 3.计算上一页
        if ( this.currentPage == this.firstPage )
            this.prePage = this.firstPage;
        else
            this.prePage = this.currentPage - 1;

        // 4.计算下一页
        if ( this.currentPage == this.totalPage )
            this.nextPage = this.totalPage;
        else
            this.nextPage = this.currentPage + 1;

        // 5.数据库查询需要的页开始索引
        this.pageBeginIndex = (this.currentPage - 1) * this.pageSize;
    }

    public PageBean(List<T> content, int currentPage, int totalCount, int pageSize) {
        this(currentPage, totalCount, pageSize);
        this.content = content;
    }


    public List<T> getContent() { return content; }
    public void setContent(List<T> content) { this.content = content; }

    public int getFirstPage() { return firstPage; }
    private void setFirstPage(){}

    public int getPrePage() { return prePage; }
    private void setPrePage(){}

    public int getNextPage() { return nextPage; }
    private void setNextPage(){}

    public int getTotalPage() { return totalPage; }
    private void setTotalPage(){}

    public int getCurrentPage() { return currentPage; }
    private void setCurrentPage(int currentPage) {}

    public int getTotalCount() { return totalCount; }
    private void setTotalCount(int totalCount) {}

    public int getPageSize() { return pageSize; }
    private void setPageSize(int pageSize) {}

    public int getPageBeginIndex() { return pageBeginIndex; }
    private void setPageBeginIndex(int pageBeginIndex) {}

}
